package com.javasm.service.B2cOrderService.impl;

import com.javasm.controller.B2CController.BaseFrontController;
import com.javasm.domin.vo.orderVo.OrderAndReceiver;
import com.javasm.service.B2cOrderService.ExportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：liyangsong
 * @Version：1.0
 * @Date：2021/11/22-10:45
 * @Since:jdk1.8
 * @Description:
 */

@Service
@Slf4j
public class ExportServiceImpl implements ExportService {
    FileOutputStream fileOutputStream = null;
    @Override
    public FileOutputStream exportExcel(List<OrderAndReceiver> orderAndReceivers) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\123_s_user_info.xls");
        try {
            fileOutputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        SXSSFWorkbook workbook = new SXSSFWorkbook(100); //每100个row自动保存到硬盘
        Sheet sheet = workbook.createSheet("表1");
             /*
                创建标题
             */
        //1.创建标题行
        Row titleRow = sheet.createRow(0);
        //2.此行创建标题列
        Cell cell = titleRow.createCell(0);
        cell.setCellValue("订单编号");
        cell = titleRow.createCell(1);
        cell.setCellValue("订单类型");
        cell = titleRow.createCell(2);
        cell.setCellValue("订单动作");
        cell = titleRow.createCell(3);
        cell.setCellValue("业务类型");
        cell = titleRow.createCell(4);
        cell.setCellValue("下单日期");
        cell = titleRow.createCell(5);
        cell.setCellValue("配送方式");
        cell = titleRow.createCell(6);
        cell.setCellValue("订单状态");
        //遍历前段发来的数据 得到信息
        for (int i = 0; i < orderAndReceivers.size(); i++) {
            OrderAndReceiver orderAndReceiver = orderAndReceivers.get(i);
            //6.为当前行的每列放入数据，此数据对应于用户信息的每个属性
            cell = titleRow.createCell(0);
            cell.setCellValue(orderAndReceiver.getId() == null ? "" : orderAndReceiver.getId().toString());
            cell = titleRow.createCell(1);
            cell.setCellValue(orderAndReceiver.getOrderType() == null ? "" : orderAndReceiver.getOrderType().toString());
            cell = titleRow.createCell(2);
            cell.setCellValue(orderAndReceiver.getOrderAction() == null ? "" : orderAndReceiver.getOrderAction());
            cell = titleRow.createCell(3);
            cell.setCellValue(orderAndReceiver.getBusinessType() == null ? "" : orderAndReceiver.getBusinessType());
//                //特殊数据类型处理
//                String birth = null;
//                if (orderAndReceiver.getCreateTime() != null){
//                }
            cell = titleRow.createCell(4);
            cell.setCellValue(String.valueOf(orderAndReceiver.getCreateTime() == null ? "" : orderAndReceiver.getCreateTime()));
            cell = titleRow.createCell(5);
            cell.setCellValue(orderAndReceiver.getDeliveryStyle() == null ? "" : orderAndReceiver.getDeliveryStyle());
            cell = titleRow.createCell(6);
            cell.setCellValue(orderAndReceiver.getOrderStatus() == null ? "" : orderAndReceiver.getOrderStatus());
            try {
                workbook.write(fileOutputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fileOutputStream;
    }

}
